<template>
  <div>
    
    <div class="flexrowbetween flexwrapper" style="align-items: flex-start">
      <!-- <div class="iconbox flexcenter commhover blackbg" @click="resetQuery">
        <i class="el-icon-refresh f-20" :class="{'rotate':isrefresh}"></i>
      </div> -->
      <div class="flexrowbetween flexwrapper full_w">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="0"
        >
          <el-form-item prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="核销状态"
              style="width: 120px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item prop="status2">
            <el-select
              v-model="queryParams.status2"
              placeholder="物流状态"
              style="width: 120px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item prop="a_id">
            <el-select
              v-model="queryParams.a_id"
              placeholder="选择核销员"
              style="width: 120px"
              clearable
            >
              <el-option
                v-for="item in userList"
                :key="item.id"
                :label="item.user_login"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="timetype"
              style="width: 100px"
            >
              <el-option label="核销时间" :value="1"></el-option>
              <el-option label="中奖时间" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item prop="day">
            <el-radio-group v-model="queryParams.day">
              <el-radio-button v-for="(item,index) in daylist" :label="item.value" :key="index">{{item.name}}</el-radio-button>
            </el-radio-group>
          </el-form-item> -->
          <el-form-item prop="day">
            <el-date-picker
              v-model="times"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="code">
            <el-input style="width:120px"
              class="searchinput"
              v-model="queryParams.code"
              placeholder="请输入券码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              class="f-14"
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >查询</el-button>
          </el-form-item>
          
          <!-- <el-form-item class="mr_0">
            <el-dropdown trigger="click">
                <el-button size="mini" class="nor_border plr_9">
                  <svg-icon class-name="f-14" icon-class="listicon" />
                  <i class="el-icon-caret-bottom el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                    <div class="dropbody">
                      <el-checkbox-group v-model="checkedOption" class="flexcolumn" @change="changeSelect">
                        <el-checkbox v-for="(item,index) in selectoptions" :label="item.label" :key="index" class="m_5">{{item.label}}</el-checkbox>
                      </el-checkbox-group>
                    </div>
                </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
          <el-form-item>
            <el-dropdown trigger="click" @command="handleCommand">
                <el-button size="mini" class="nol_border plr_9">
                  <svg-icon class-name="f-14" icon-class="export" />
                  <i class="el-icon-caret-bottom el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item,index) in exportlist" :key="index" :command="item.id">{{item.name}}</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
          </el-form-item> -->
          <el-form-item>
            <el-button
              class="f-14"
              type=""
              :loading='exportloading'
              size="mini"
              @click="handleExport"
            >导出</el-button>
          </el-form-item>
        </el-form>
        <el-button plain type="primary" class="gohexiao" size="mini" @click="handleOpenmut">批量核销</el-button>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      ref="tablelayout"
      class="commtable"
      @selection-change="handleSelectionChange"
    >
    <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="平台" prop="platName" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.task.tp_name || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="活动分类" width="120" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.level_name || '-'}} </span>
        </template>
      </el-table-column>
      <el-table-column
        label="活动名称"
        prop="task.name"
        min-width="280"
        align="center"
      />
      <el-table-column
        label="活动编号"
        prop="task_id"
        width="120"
        align="center"
      />
      <el-table-column
        label="核销时间"
        prop="last_time"
        width="260"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.last_time">
            <span v-if="scope.row.address_time">{{ new Date(scope.row.last_time).getTime() > new Date(scope.row.address_time).getTime() ? scope.row.last_time : scope.row.address_time  }}</span>
            <span v-else>{{scope.row.last_time||'-'}}</span>
          </span>
          <span v-else-if="scope.row.address_time">{{scope.row.address_time||'-'}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="中奖时间"
        prop="create_time"
        width="260"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{scope.row.create_time||'-'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="前台核销员" prop="time" width="200" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.member?scope.row.member.user_login:'-'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="后台核销员" prop="time" width="200" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.admin?scope.row.admin.user_login:'-'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="券码" prop="code" width="200" align="center" />
      <el-table-column
        label="核销方式"
        prop="time"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          <!-- <el-tag  v-if="scope.row.draw.draw_type==1" type="warning">线上</el-tag> -->
          <el-tag  v-if="scope.row.draw.draw_type==2" type="success">线下</el-tag>
          <el-tag  v-else type="success">线上</el-tag>
          <!-- <span>{{scope.row.draw.draw_type_name}}</span> -->
          <!-- <span v-if="scope.row.draw.draw_type==1">线上</span>
          <span v-if="scope.row.draw.draw_type==2">线下</span> -->
        </template>
      </el-table-column>
      <el-table-column
        label="奖项等级"
        prop="draw.level_name"
        width="200"
        align="center"
      />
      <el-table-column
        label="奖品名称"
        prop="draw.draw_name"
        width="220"
        align="center"
      />
      <el-table-column label="中奖者" prop="time" width="200" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.wxuser.name || '-'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="核销状态" align="center" width="150">
        <template slot-scope="scope">
          <div v-if="scope.row.is_cash==1">
            <div v-if="!scope.row.more">
              <el-tag type="info" class="tag_info">未核销</el-tag>
            </div>
            <div v-else>
              <el-tag type="info" class="tag_info" v-if="scope.row.status == 0">未核销</el-tag>
              <el-tag type="success" class="tag_success" v-else>已核销</el-tag>
            </div>
          </div>
          <div v-else>
            <el-tag type="info" class="tag_info" v-if="scope.row.status == 0">未核销</el-tag>
            <el-tag type="success" class="tag_success" v-else>已核销</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="物流状态" align="center" width="150">
        <template slot-scope="scope">
          <!-- 上门自取为- -->
          <div v-if="scope.row.is_cash == 2">-</div> 
          <div v-else>
            <div v-if="scope.row.more">
              <el-tag v-if="scope.row.status == 0 || scope.row.status == 2" type="warning" class="tag_warning">待发货</el-tag>
              <el-tag v-if="scope.row.status == 3" type="success" class="tag_success">已发货</el-tag>
            </div>
            <div v-else>-</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="物流信息" width="300" align="center" fixed="right">
        <template slot-scope="scope">
          <div class="flex">
            <div v-if="scope.row.is_cash==1"
              class="commhover btn_green mr_10 c_fff"
              @click="handleAdd(scope.row)"
            >
             添加物流单
            </div>
            <!-- <div v-if="scope.row.is_cash==1" -->
            <div v-if="scope.row.track_name && scope.row.track_number"
              class="commhover btn_blue c_fff mr_10"
              @click="handleDetail(scope.row)"
            >
             详情
            </div>
            <div v-if="scope.row.is_cash == 2">
              <div v-if="scope.row.status == 0" class="commhover btn_yellow mr_10 c_fff" @click="handleOpen(scope.row,1)"> 核销 </div>
              <div v-else class="commhover btn_yellow mr_10 c_fff" @click="handleOpen(scope.row,2)">撤销核销</div>
            </div>
            <div v-else>
              <div class="commhover btn_yellow mr_10 c_fff" @click="handleOpen(scope.row)"> 核销 </div>
            </div>
            <!-- <div
              class="commhover btn_green mr_10 c_fff"
              @click="handleFabu(scope.row)"
            >
              添加地址
            </div> -->
          </div>
        </template>
      </el-table-column>
      <el-table-column label="备注" width="150" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.more">{{ scope.row.more.beizhu }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.num"
      @pagination="getList"
    />

    <el-dialog
      title="添加物流单"
      :visible.sync="open"
      width="680px"
      class="dialogheaderbg"
      :before-close="handleClose"
      :close-on-click-modal='false'
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="快递公司" prop="track_name">
          <el-input v-model="form.track_name" placeholder="请输入快递公司" />
          <!-- <el-select
            clearable
            v-model="form.track_name"
            placeholder="请选择快递公司"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in kdList"
              :key="index"
              :value="item.value"
              >{{ item.label }}</el-option
            >
          </el-select> -->
        </el-form-item>
        <el-form-item label="快递单号" prop="track_number">
          <el-input
            v-model="form.track_number"
            placeholder="请输入快递单号"
            maxlength="50"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog
      title="物流详情"
      :visible.sync="detailOpen"
      width="760px"
      class="dialogheaderbg"
      :before-close="handleDetailClose"
      :close-on-click-modal='false'
    >
      <view-comp v-if="detailOpen" :row="row" @close="handleDetailClose" />
    </el-dialog>

    <!-- 核销 -->
    <el-dialog
      title="核销奖券"
      :visible.sync="hexopen"
      width="900px"
      class="dialogheaderbg"
      :before-close="handleClosehx"
      :close-on-click-modal='false'
    >
      <hexiao-comp v-if="hexopen" :row='row' @close="handleClosehx" />
    </el-dialog>

    <el-dialog
      title="批量核销"
      :visible.sync="mutilopen"
      width="620px"
      class="dialogheaderbg"
      :before-close="handleClosemut"
      :close-on-click-modal='false'
    >
      <batch-ver v-if="mutilopen" :rowids='rowids' @close="handleClosemut"/>
    </el-dialog>
  </div>
</template>
<script>
// import { mapState } from "vuex";
// import FabuComp from "./components/FabuComp.vue";
// import LogTable from "./components/LogTable.vue";
import BatchVer from './components/BatchVer.vue'
import HexiaoComp from './components/HexiaoComp.vue'
import ViewComp from "./components/ViewComp.vue";
import { drawIndex,codeOrder,destroyCode,indexExport,revocationCode } from '@/api/hexiao'
export default {
  data() {
    return {
      timetype:1,
      queryParams: {
        page: 1,
        num: 10,
        status: undefined,
        a_id: undefined,
        code: undefined
      },
      total: 1,
      loading: false,
      tableData: [],
      rowids:[],
      // 非单个禁用
      single: true,
      multiple:true,
      options: [
        // { label: "全部核销状态", value: 0 },
        { label: "已核销", value: 1 },
        { label: "未核销", value: 0 },
        { label: "代发货", value: 2 },
        { label: "已发货", value: 3 },
      ],
      userList:[],
      daylist: [
        { name: "今天", value: 1 },
        { name: "昨天", value: 2 },
        { name: "最近7天", value: 7 },
        { name: "最近30天", value: 30 },
        { name: "本月", value: 31 },
      ],
      times:'',
      isrefresh: false,
      open: false,
      form:{
        track_number:'',
        track_name:'',
        id:''
      },
      rules:{
        track_name:[{ required: true, message: "请输入快递公司", trigger: "blur" }],
        track_number:[{ required: true, message: "请输入快递单号", trigger: "blur" }],
      },
      // kdList:[{label:"顺丰快递",value:1}],
      detailOpen:false,
      rowId:'',
      hexopen:false,
      row:'',
      mutilopen:false,
      exportloading:false
    };
  },

  props: {
    type: {
      type: Number,
      default: 1,
    },
  },

  components:{
    ViewComp,
    BatchVer,
    HexiaoComp
  },

  created() {
    this.getDicts('/manage/index/adminList').then(res=>{
      this.userList = res.data
    })
    this.getList()

  },

  methods: {
    getList() {
      this.loading = true;
      let params = { 
        page:this.queryParams.page,
        num:this.queryParams.num  
      };
      if(this.queryParams.code){
        params.code = this.queryParams.code
      }
      if(this.queryParams.a_id){
        params.a_id = this.queryParams.a_id
      }
      if(this.queryParams.status || this.queryParams.status == 0){
        params.status = this.queryParams.status
      }
      // 
      console.log('this.time:',this.times)
      if(this.times && this.times.length){
        if(this.timetype == 1){
          params.last_startime = this.times[0]
          params.last_endtime = this.times[1]
        }else{
          params.create_startime = this.times[0]
          params.create_endtime = this.times[1]
        }
      }
      drawIndex(params).then((response) => {
        this.tableData = response.data.list;
        this.total = response.data.count;
        this.loading = false;
        this.isrefresh = false;
      })
    },

    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },

    resetQuery() {
      // this.isrefresh = true;
      this.times = 1
      this.resetForm("queryForm");
      this.handleQuery();
    },

    changeSelect(arr) {
      this.$store.commit("SET_ACTIVEOPTION", arr);
      this.$nextTick(() => {
        //在数据加载完，重新渲染表格
        this.$refs.tablelayout && this.$refs.tablelayout.doLayout();
      });
    },

    handleCommand(command) {
      console.log(command);
    },

    handleSelectionChange(selection) {
      this.rowids = selection.map((item) => item.task_id);
      this.multiple = !selection.length;
      this.single = selection.length!=1
    },

    handleAdd(row) {
      if(!row.more){
        this.$message.warning('请先填写收货地址')
        return
      }
      this.row = row
      this.form.id = row.id
      this.form.track_name = row.track_name
      this.form.track_number = row.track_number
      this.open = true;
    },

    handleClose() {
      this.open = false;
    },

    submit(){
      this.$refs.form.validate(valid=>{
        if(!valid) return
        codeOrder(this.form).then(res=>{
          this.$message.success('添加成功')
          this.handleClose()
          this.getList()
        })
      })
    },

    handleDetail(row){
      this.row = row
      this.detailOpen = true
    },

    handleDetailClose(){
      this.detailOpen = false
    },

    handleOpen(row,type){
      this.row = row
      if(row.is_cash==2){
        let info = `确定将该条的数据${type==1?'':'撤销'}核销吗？`
        this.$confirm(info, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            if(type==1){
              destroyCode({
                id: row.id,
              }).then(() => {
                this.$modal.msgSuccess("核销成功");
                this.getList();
              });
            }else{
              revocationCode({
                id: row.id,
              }).then(() => {
                this.$modal.msgSuccess("撤销核销成功");
                this.getList();
              });
            }
            
          })
          .catch(() => {});
      }else{
        this.hexopen = true
      }
    },

    handleClosehx(type){
      if(type==1){
        this.getList()
      }
      this.hexopen = false
    },

    handleOpenmut(){
      this.mutilopen = true
    },

    handleClosemut(type){
      if(type == 1){
        this.getList()
      }
      this.mutilopen = false
    },

    handleExport(){
      let params = {
        head:['平台','活动名称','活动编号','核销时间','中奖时间','后台核销员','前台核销员','券码','核销方式','奖项等级','奖品名称','中奖者','核销状态'],
        keys:['tp_task_name','task.name','task.id','last_time','create_time','admin.user_login','member.user_login','code','draw.draw_type_name','draw.level_name','draw.draw_name','wxuser.name','status_name'],
      }
      if(this.queryParams.code){
        params.code = this.queryParams.code
      }
      if(this.queryParams.a_id){
        params.a_id = this.queryParams.a_id
      }
      if(this.queryParams.status || this.queryParams.status == 0){
        params.status = this.queryParams.status
      }
      if(this.time && this.time.length){
        if(this.timetype == 1){
          params.last_startime = this.time[0]
          params.last_endtime = this.time[1]
        }else{
          params.create_startime = this.time[0]
          params.create_endtime = this.time[1]
        }
      }
      this.exportloading = true
      indexExport(params).then(res=>{
        let url = res.data
        this.$message.success("正在下载，请稍后");
        var a = document.createElement("a");
        a.setAttribute("href", url);
        a.click();
        this.exportloading = false
      })
    }

  },
};
</script>

<style lang="scss" scoped>
.iconbox {
  width: 32px;
  height: 26px;
}
.imgmini {
  width: 40px;
  height: 40px;
  margin: auto;
}
.imgbox {
  width: 120px;
  height: 120px;
}
.gohexiao{
  margin-bottom: 18px;
//   position: absolute;
//   right: 15px;
//   top: 15px;
}
</style>
<style lang="scss">
.noheaderdia {
  .el-dialog__header {
    padding: 0 !important;
  }
  .el-dialog__body {
    padding: 0 !important;
  }
}
</style>